import Vue from 'vue'
import Router from 'vue-router'
import hideManageRoute from './hideManageRouter' // 导入路由文件
import testing from './testing.js'
import safeCheck from './safeCheck' // 安全检查
import safeRisk from './safeRisk' // 安全风险管理
import system from './system' // 制度管理
import indexManageRouter from './indexManageRouter' // 导入首页路由文件
import peopleManageRouter from './peopleManageRouter' // 导入人员管理路由文件
import workrisk from './workrisk'
import equipmentManageRouter from './equipmentManageRouter' // 设配管理
import accidentReportRouter from './accidentReportRouter'
import safetyManage from './safetyManageRouter' // 安全
import teamActivity from './teamActivity' // 班组活动
import technicalSolution from './technicalSolutionRouter' // 技术方案
import informRouter from './informRouter' // 通知公告
import safeExposureRouter from './safeExposureRouter' // 安全曝光
import constructionElectricityRouter from './constructionElectricityRouter' // 施工用电
import safeResponsibilityRouter from './safeResponsibilityRouter' // 安全责任制
import professionalRecordRouter from './professionalRecordRouter' // 履职记录
import leadershipWorkRouter from './leadershipWorkRouter' // 领导带班
import safetyWarningRouter from './safetyWarningRouter' // 安全预警
import safetyTargetRouter from './safetyTargetRouter' // 安全指标
import meetingNoticeRouter from './meetingNoticeRouter' // 安全生产例会
import emergencyRescueRouter from './emergencyRescueRouter' // 应急救援
import occupationalHealthRouter from './occupationalHealthRouter' // 职业危害因素监测
let router = [{
    path: '/rootIndex',
    name: 'rootIndex',
    component: (resolve) => require(['@/components/rootIndex'], resolve),
    meta: {
        title: '首页'
    }
},
{
    path: '/',
    redirect: {
        name: 'IndexManager'
    }
},
{
    path: '/Index',
    name: 'Index',
    component: (resolve) => require(['@/components/Index'], resolve),
    children: [{
        path: '/',
        redirect: {
            name: 'IndexManager'
        }
    },
    {
        path: 'IndexManager',
        name: 'IndexManager',
        component: (resolve) => require(['@/components/IndexManager/IndexManager'], resolve),
        meta: {
            title: '首页'
        }
    },
    {
        path: '/editchange',
        name: 'editchange',
        component: (resolve) => require(['@/components/workmask/editchange'], resolve),
        meta: {
            title: '我的应用'
        }
    },
    {
        path: 'newEdit',
        name: 'newEdit',
        component: (resolve) => require(['@/components/workmask/newEdit'], resolve),
        meta: {
            title: '工作台'
        }
    },
    {
        path: 'userinfo',
        name: 'userinfo',
        component: (resolve) => require(['@/components/UserCenter/userinfo'], resolve),
        meta: {
            title: '个人中心'
        }
    }
    ]
}
]
let routes = new Set([...indexManageRouter, ...hideManageRoute, ...system, ...safeCheck, ...safeRisk, ...router, ...testing, ...peopleManageRouter, ...workrisk, ...accidentReportRouter, ...equipmentManageRouter, ...safetyManage, ...teamActivity, ...technicalSolution, ...informRouter, ...safeExposureRouter, ...constructionElectricityRouter, ...safeResponsibilityRouter, ...professionalRecordRouter, ...leadershipWorkRouter, ...safetyWarningRouter, ...safetyTargetRouter, ...meetingNoticeRouter, ...emergencyRescueRouter, ...occupationalHealthRouter])
Vue.use(Router)

let newRouter = new Router({
    routes
    // base: '/history',
    // mode: 'history'
})
newRouter.beforeEach((to, from, next) => {
    let userInfo = localStorage.getItem('userInfo') // 为登录
    let loginInfo = localStorage.getItem('loginInfo') // 为登录
    if (from.fullPath != '/hideRegister') {
        window.scrollTo(0, 0)
    }
    if (to.meta.title) {
        document.title = to.meta.title
    }
    if (to.path === '/addUser' && !to.query.redirect && from.fullPath.indexOf('/addUser') == -1 && from.fullPath.indexOf('/addCompany') == -1) {
        next({
            path: '/addUser',
            query: {
                redirect: from.fullPath,
                departID: to.query.departID,
                departName: to.query.departName,
                endTime: to.query.endTime,
                Category: to.query.departSir
            } // 将跳转的路由path作为参数，登录成功后跳转到该路由
        })
    } else if (!userInfo && !loginInfo && from.path === '/addUser' && to.path !== '/addCompany') {
        next({
            path: from.fullPath
        })
    } else {
        next()
    }
})

export default newRouter
